package commons

import (
	"github.com/kataras/iris"
	"irisfarm/app/core/output"
)

func ErrorHandle(app *iris.Application) {
	httpError := HttpError{}

	app.OnErrorCode(iris.StatusNotFound, httpError.notFound)

	app.OnErrorCode(iris.StatusInternalServerError, httpError.internalServerError)
}

type HttpError struct {
}

func (h *HttpError) notFound(ctx iris.Context) {
	o := output.Output{Ctx: ctx}
	o.Error(404, "404 not found")
}

func (h *HttpError) internalServerError(ctx iris.Context) {
	o := output.Output{Ctx: ctx}
	o.Error(500, "Oops something went wrong, try again")
}

func (h *HttpError) AuthError(ctx iris.Context, msg string) {
	o := output.Output{Ctx: ctx}

	if msg == "" {
		o.Error(401, "Oops auth fail")
	} else {
		o.Error(401, msg)
	}
}
